if [ -e /dev/tcaicard0 ];then
        AICARD_NUM=`ls /dev/tcaicard* | wc -l`
else
        echo "No card !"
        exit
fi

if [ $# == 0 ];then

    echo "没有带参数"
    card_num=4

else

    echo "带了$#个参数"
    card_num=$1

fi


PSY_AICARD_NUM_1=`expr $AICARD_NUM / 4`
let PSY_AICARD_NUM=${PSY_AICARD_NUM_1}-1

CURTIME=`date +%m%d%H%M`
echo $CURTIME
CUR_PATH=`pwd`
SAVEDLOG=$CUR_PATH/4COREPCIE/$CURTIME
log_file=$SAVEDLOG/4CORE_PCIE.log_all

mkdir -p $SAVEDLOG
if [ -e $log_file ];then
    rm -rf $log_file
fi

touch $log_file

echo $log_file

if [[ $PSY_AICARD_NUM_1 < $card_num ]];
then
	echo "unknown card!"
	echo "card num is $PSY_AICARD_NUM_1" | tee -a $log_file
	echo "test FAILED" | tee -a $log_file
        exit 1
fi

sh test_pci.sh
res=$(sh test_pci.sh | grep -c 'Speed 16GT/s, Width x16')

length=$(expr length "$res")
echo $res

datanum=`expr $card_num \* 2`
echo "dat is $datanum"

if [[ $res -ne $datanum ]];
then
        echo -e "ALL CARD CHECK PCI RESULT: \E[31m [FAILED] \E[0m" | tee -a $log_file
        exit

else
        echo -e "ALL CARD CHECK PCI RESULT: \E[32m [PASS] \E[0m" | tee -a $log_file
fi

echo "run tccl"
cd torchTest
SDAA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 python3 ddp_autotest.py | tee -a $log_file

res=$(cat test_log/result_torch.txt | grep 'failed')
length=$(expr length "$res")
echo $length

if [[ length -gt 0 ]];
then
        echo -e "TCCL_FUNC TEST RESULT: \E[31m [FAILED] \E[0m" | tee -a $log_file
else
        echo -e "TCCL TEST RESULT: \E[32m [PASS] \E[0m" | tee -a $log_file
fi
cd -

for card_id in `seq 0 ${PSY_AICARD_NUM}`
do
    start_device=`expr $card_id \* 4`
    speclk ${start_device} 4  | tee -a $log_file
    result=$(speclk ${start_device} 4 | grep -c 'N/A')


    length=$(expr length "$result")
    echo "result is $result"

   if [[ $result -ne 0 ]];
   then
       echo -e "ALL CARD CHECK SN RESULT: \E[31m [FAILED] \E[0m" | tee -a $log_file
       exit

       echo "111"
   else
       echo -e "ALL CARD CHECK SN RESULT: \E[32m [PASS] \E[0m" | tee -a $log_file
fi
done

# for card_id in `seq 0 ${PSY_AICARD_NUM}`
# do
#     start_device=`expr $card_id \* 4`
#     ./mem ${start_device}  | tee -a $log_file
# done

###### run function #############
cd function
sh PCIE_4CORE_FUNC.sh $SAVEDLOG | tee -a $log_file
cd -
#exit

result=$(cat $log_file | grep  'FAILED')
length=$(expr length "$result")
if [[ length -gt 0 ]];
then
        echo -e "CARD TEST RESULT: \E[31m [FAILED] \E[0m" | tee -a $log_file
        exit
else
        echo -e "CARD TEST RESULT: \E[32m [PASS] \E[0m" | tee -a $log_file
fi

cat $log_file | grep  'FAILED'

###### run full_load #############
cd full_load/build
for card_id in `seq 0 ${PSY_AICARD_NUM}`
do
    start_device=`expr $card_id \* 4`
    speclk ${start_device} 4  | tee -a $log_file
    result=$(speclk ${start_device} 4 | grep -c 'N/A')


    length=$(expr length "$result")
    echo "result is $result"

   if [[ $result -ne 0 ]];
   then
       echo -e "ALL CARD CHECK SN RESULT: \E[31m [FAILED] \E[0m" | tee -a $log_file
       exit

       echo "111"
   else
       echo -e "ALL CARD CHECK SN RESULT: \E[32m [PASS] \E[0m" | tee -a $log_file
   fi

   speclk ${start_device} 1  | tee -a $log_file
   sleep 1
   sh fullLoad_run.sh $card_id 1 30 ${sn}.log | tee -a $log_file &
done

wait
cd -

sleep 20

###### run ngc_r50 #########
cd ngc_resnet/run_scripts/
for card_id in `seq 0 ${PSY_AICARD_NUM}`
do
   start_device=`expr $card_id \* 4`
   speclk ${start_device} 0  | tee -a $log_file
   sh single_card.sh $card_id | tee -a $log_file &
done
wait

result=$(cat $log_file | grep  'FAILED')
length=$(expr length "$result")
if [[ length -gt 0 ]];
then
        echo -e "CARD TEST RESULT: \E[31m [FAILED] \E[0m" | tee -a $log_file
        exit
else
        echo -e "CARD TEST RESULT: \E[32m [PASS] \E[0m" | tee -a $log_file
fi

cat $log_file | grep  'FAILED'

echo "function finished"
